package com.xnx3.wangmarket.shop.api.controller;

import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xnx3.banmahuishou.core.service.PriceService;
import com.xnx3.banmahuishou.core.vo.PriceVO;
import com.xnx3.j2ee.entity.User;
import com.xnx3.j2ee.pluginManage.controller.BasePluginController;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.ActionLogUtil;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.shop.core.entity.GoodsType;
import com.xnx3.wangmarket.shop.core.service.GoodsService;
import com.xnx3.wangmarket.shop.core.vo.GoodsTypeListVO;

/**
 * 商品分类相关
 * @author 管雷鸣
 */
@Controller(value="ShopGoodsTypeController")
@RequestMapping("/shop/api/goodsType/")
public class GoodsTypeController extends BasePluginController {
	@Resource
	private SqlService sqlService;
	@Resource
	private GoodsService goodsService;
	@Resource
	private PriceService priceService;

	/**
	 * 获取某个店铺的商品分类
	 * @param storeid 店铺id，对应 Store.id ,要获取的是哪个店铺的商品分类
	 * @return {@link GoodsTypeListVO} 若不存在，则会new一个返回过来，不会存在null的情况
	 * @author 管雷鸣
	 */
	@RequestMapping(value="list.json", method = RequestMethod.POST)
	@ResponseBody
	public GoodsTypeListVO list(HttpServletRequest request,
			@RequestParam(value = "storeid", required = true, defaultValue="1") int storeid){

		GoodsTypeListVO vo = new GoodsTypeListVO();

		if(storeid == 0){
			vo.setBaseVO(GoodsTypeListVO.FAILURE, "请传入要获取的店铺编号 storeid");
			return vo;
		}

		List<GoodsType> list = sqlService.findBySqlQuery("SELECT * FROM shop_goods_type WHERE storeid = "+storeid+" AND isdelete = "+GoodsType.ISDELETE_NORMAL +" ORDER BY rank ASC", GoodsType.class);
		vo.setList(list);

		User user = sqlService.findById(User.class, getUserId());
		if (user != null) {
			if (Objects.equals(user.getNickname(), "nickname")) {
				vo.setIsHave(0);
			}else {
				vo.setIsHave(1);
			}
		}else {
			vo.setIsHave(0);
		}

		ActionLogUtil.insert(request, storeid,"获取店铺内商品的分类列表");	//日志记录
		return vo;
	}



	/**
	 * 获取选择的大类的预估价格（下单填写-添加废品使用）
	 * @param goodsTypeId 要添加废品id，对应goodsType.id
	 */
	@RequestMapping(value="getGoodsTypePrice.json",method = {RequestMethod.POST})
	@ResponseBody
	public PriceVO getGoodsTypePrice(HttpServletRequest request,
			@RequestParam(value = "goodsTypeId",required = true, defaultValue = "12") int goodsTypeId){

		User user = sqlService.findById(User.class, getUserId());
		PriceVO priceVO = new PriceVO();
		if(user == null) {
			priceVO.setBaseVO(BaseVO.FAILURE, "请重新扫码登录");
			return priceVO;
		}
		//计算价格，获取预估价
		int dlsUserid = user.getDailishangUserid() == null ? 0 : user.getDailishangUserid();
		priceVO = priceService.getPriceByGoodsTypeId(goodsTypeId,dlsUserid);
		if(priceVO.getResult() - PriceVO.FAILURE == 0) {
			priceVO.setBaseVO(BaseVO.FAILURE, priceVO.getInfo());
			return priceVO;
		}
		
		return priceVO;
	}
}